package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.service.BaseCategory1Service;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.album.service.BaseCategoryViewService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.model.search.AttributeValueIndex;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


@Tag(name = "分类管理")
@RestController
@RequestMapping(value="/api/album/category")
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryApiController {
	
	@Autowired
	private BaseCategoryService baseCategoryService;
	@Autowired
	private BaseCategory1Service baseCategory1Service;
	@Autowired
	private BaseCategoryViewService baseCategoryViewService;
	@Operation(summary = "查询所有1的一级分类id集合")
	@GetMapping("/findAllC1Ids")
	public Result<List<Long>> findAllC1Ids(){
		return Result.ok(baseCategory1Service
				.list(Wrappers.lambdaQuery(BaseCategory1.class)
						.select(BaseCategory1::getId))
				.stream()
				.map(BaseCategory1::getId)
				.collect(Collectors.toList()));
	}
	///api/album/category/getBaseCategoryList/2
	@Operation(summary = "根据1级分类id查询分类下的二级三级分类嵌套数据")
	@GetMapping("getBaseCategoryList/{c1Id}")
	public Result getBaseCategoryListByC1Id(@PathVariable("c1Id")Long c1Id){
		//返回1级分类对象： categoryName  categoryId categoryChild
		JSONObject jsonObject =
		baseCategoryService.getBaseCategoryListByC1Id(c1Id);
		return Result.ok(jsonObject);
	}
	///api/album/category/findTopBaseCategory3/3
	@Operation(summary = "根据1级分类id查询 前7个三级分类列表")
	@GetMapping("findTopBaseCategory3/{c1Id}")
	public Result findTopBaseCategory3(@PathVariable("c1Id")Long c1Id){
		List<BaseCategory3> list = baseCategoryService.findCategory3sByC1Id(c1Id);
		return Result.ok(list);
	}

	@Operation(summary = "内部接口：根据1级分类id查询前7个三级分类列表")
	@GetMapping("findCategory3sByC1Id/{c1Id}")
	public Result<List<BaseCategory3>> findCategory3sByC1Id(@PathVariable("c1Id")Long c1Id){
		List<BaseCategory3> list = baseCategoryService.findCategory3sByC1Id(c1Id);
		return Result.ok(list);
	}
	@Operation(summary = "内部接口：根据3级分类id查询专辑分类数据")
	@GetMapping("findAttributeByC3Id/{c3Id}")
	public Result<BaseCategoryView> findAttributeByC3Id(@PathVariable("c3Id")Long c3Id){
		BaseCategoryView baseCategoryView = baseCategoryViewService.getOne(Wrappers.lambdaQuery(BaseCategoryView.class)
				.eq(BaseCategoryView::getCategory3Id , c3Id));
		return Result.ok(baseCategoryView);
	}
	//findAttribute/2
	@Operation(summary = "根据一级分类id查询专辑可选的属性和值")
	@GetMapping("findAttribute/{c1Id}")
	public Result<List<BaseAttribute>> findAttribute(@PathVariable("c1Id")Long c1Id){
		List<BaseAttribute> baseAttributes =  baseCategoryService.findAttribute(c1Id);
		return Result.ok(baseAttributes);
	}

	//getBaseCategoryList
	//Result；统一响应类(code message  data)
	@Operation(summary = "创建专辑查询专辑分类列表")
	@GetMapping("getBaseCategoryList")
	public Result getBaseCategoryList(){
		/*
			页面需要的数据，没有现有的实体类描述
			可以自定义VO类  也可以使用Map  也可以使用JSONObject
			JSONObject应该包含： categoryName、categoryId、categoryChild三个属性
		 */
		List<JSONObject> list =  baseCategoryService.getBaseCategoryList();
		return Result.ok(list);
	}
}

